package com.yanqu.road.server.manager.activity.biaoju;

import com.yanqu.road.dao.impl.activity.biaoju.CrossBiaoJuCityImpl;
import com.yanqu.road.dao.impl.activity.biaoju.CrossBiaoJuUnionImpl;
import com.yanqu.road.entity.activity.biaoju.CrossBiaoJuCityData;
import com.yanqu.road.entity.activity.biaoju.CrossBiaoJuUnionData;
import com.yanqu.road.entity.activity.biaoju.config.BiaoJuCityConfig;
import com.yanqu.road.logic.activity.biaoju.BiaoJuConfig;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossBiaoJuUnionInfo {
    private CrossBiaoJuUnionData unionData;
    /**
     * key: userId
     */
    private Map<Long, CrossBiaoJuUserInfo> memberInfoMap = new ConcurrentHashMap<>();
    /**
     * key: cityId
     */
    private Map<Integer, CrossBiaoJuCityData> cityDataMap = new ConcurrentHashMap<>();

    private long rankScore;
    private long scoreChangeTime;

    public CrossBiaoJuUnionInfo(CrossBiaoJuUnionData unionData) {
        this.unionData = unionData;
    }

    public void save() {
        if (unionData.isInsertOption()) {
            new CrossBiaoJuUnionImpl().add(unionData);
        } else if (unionData.isUpdateOption()) {
            new CrossBiaoJuUnionImpl().update(unionData);
        }

        List<CrossBiaoJuUserInfo> memberInfoList = getMemberInfoList();
        for (CrossBiaoJuUserInfo biaoJuUserInfo : memberInfoList) {
            biaoJuUserInfo.save();
        }

        for (CrossBiaoJuCityData cityData : cityDataMap.values()) {
            if (cityData.isInsertOption()) {
                new CrossBiaoJuCityImpl().add(cityData);
            } else if (cityData.isUpdateOption()) {
                new CrossBiaoJuCityImpl().update(cityData);
            }
        }
    }

    public void addUserInfo(CrossBiaoJuUserInfo userInfo) {
        synchronized (memberInfoMap) {
            memberInfoMap.put(userInfo.getUserData().getUserId(), userInfo);
        }
    }

    public List<CrossBiaoJuUserInfo> getMemberInfoList() {
        synchronized (memberInfoMap) {
            return new ArrayList<>(memberInfoMap.values());
        }
    }


    public void initCityData(BiaoJuConfig config) {
        for (BiaoJuCityConfig cityConfig : config.getCityConfigMap().values()) {
            CrossBiaoJuCityData cityData = new CrossBiaoJuCityData(unionData.getActivityId(), cityConfig.getId(), unionData.getUnionId(), unionData.getUnionGroup());
            cityDataMap.put(cityData.getCityId(), cityData);
        }
    }

    public CrossBiaoJuUnionData getUnionData() {
        return unionData;
    }

    public void setUnionData(CrossBiaoJuUnionData unionData) {
        this.unionData = unionData;
    }

    public Map<Long, CrossBiaoJuUserInfo> getMemberInfoMap() {
        return memberInfoMap;
    }

    public void setMemberInfoMap(Map<Long, CrossBiaoJuUserInfo> memberInfoMap) {
        this.memberInfoMap = memberInfoMap;
    }

    public Map<Integer, CrossBiaoJuCityData> getCityDataMap() {
        return cityDataMap;
    }

    public void setCityDataMap(Map<Integer, CrossBiaoJuCityData> cityDataMap) {
        this.cityDataMap = cityDataMap;
    }

    public synchronized void addExp(long addValue, BiaoJuConfig config) {
        unionData.addExp(addValue);
        int newLevel = config.calcUnionLevel(unionData.getExp());
        unionData.setLevel(newLevel);
    }

    public void refreshRankParam() {
        this.rankScore = unionData.getExp();
        this.scoreChangeTime = unionData.getExpChangeTime();
    }

    public long getRankScore() {
        return rankScore;
    }

    public long getScoreChangeTime() {
        return scoreChangeTime;
    }

    public synchronized void setRank(int rank) {
        unionData.setRank(rank);
    }

    public List<CrossBiaoJuCityData> getCityDataList() {
        return new ArrayList<>(cityDataMap.values());
    }
}
